package org.opens.mybatisplus.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RestController(value = "vedioController")
@RequestMapping(value = "/api/vedio")
public class VedioController {

    @RequestMapping(value = "/play")
    public void playVideo(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        File file = new File("D:\\demo.mp4");
        String fileName = file.getName();
        String userAgent = request.getHeader("User-Agent").toLowerCase();

        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        //设置response编码
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Length", "" + file.length());
        //设置输出文件类型
        response.setContentType("video/mpeg4");

        FileInputStream fis = null;
        OutputStream os = null;

        //获取response输出流
        try {
            os = response.getOutputStream();
            fis = new FileInputStream(file);
            IOUtils.copy(fis, os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis);
            IOUtils.closeQuietly(os);
        }

    }

}
